<?php

class WeixinOpen extends OpenBase
{
	private $weixin = null;
	private $sogou = null;
	private $mongo = null;

	public function __construct()
	{
		parent::__construct();
		$this->weixin = n_api('weixin');
		$this->sogou = n_api('sogou');
		$this->mongo = new MongoClient("mongodb://newbee:9^4%NbAdwS@115.28.18.40:27017");
	}

	public function getZeroWeixin()
	{
		$param = $this->param('clientIp:char:empty,skip:char:empty,limit:char:empty');
		$clientIp = $param['clientIp'];
        $skip = $param['skip'];
        $limit = $param['limit'];

        $rs = $this->weixin->getWeixinList($skip, $limit, 0);
        $weixinList = [];
        foreach ($rs as $value) {
        	$tmp['url'] = 'http://weixin.sogou.com/weixin?type=1&query=' . urlencode($value['wxid']) . '&ie=utf8';
          $tmp['wxid'] = $value['wxid'];
          $tmp["face_url"] = $value['face_url'];
          $weixinList[] = $tmp;
        }
        echo json_encode($weixinList);
	}

	public function getAllWeixin()
	{
		$param = $this->param('clientIp:char:empty,skip:char:empty,limit:char:empty');
		$clientIp = $param['clientIp'];
        $skip = $param['skip'];
        $limit = $param['limit'];

		$rs = $this->weixin->getWeixinList($skip, $limit);
		$weixinList = [];
		foreach ($rs as $value) {
			$tmp['wxid'] = $value['wxid'];
			$tmp['biz'] = $value['biz'];
			$weixinList[] = $tmp;
		}
		echo json_encode($weixinList);
	}

	public function getWeixinFace()
	{
		$param = $this->param('clientIp:char:empty,skip:char:empty,limit:char:empty');
		$clientIp = $param['clientIp'];
        $skip = $param['skip'];
        $limit = $param['limit'];

		$rs = $this->weixin->getWeixinList($skip, $limit);
		$weixinList = [];
		foreach ($rs as $value) {
			$tmp['wxid'] = $value['wxid'];
			$tmp['faceUrl'] = $value['face_url'];
			$weixinList[] = $tmp;
		}
		echo json_encode($weixinList);
	}

	public function getImgUrlList() {
		$param = $this->param('clientIp:char:empty,skip:char:empty,limit:char:empty');
		$clientIp = $param['clientIp'];
    $skip = $param['skip'];
    $limit = $param['limit'];

    $articleCollection = $this->mongo->selectDB('weixin')->selectCollection('article');
    $stime = strtotime(date('Y-m-d', strtotime('-1 day')));

    $query = ['ptime' => ['$gte' => $stime], 'flag' => 0];
    $cursor = $articleCollection->find($query, ['cover'=>1])->skip($skip)->limit($limit);
		$data = [];
		foreach ($cursor as $value) {
			$tmp['cover'] = $value['cover'];
			$tmp['mongoid'] = $value['_id']->{'$id'};
			$data[] = $tmp;
		}
		echo json_encode($data);
	}

	public function setReadNow() {
		$redis = n_api('redis')->scrapy;
		$key = "readNow";
		$redis->set($key, 1);
	}

	public function unsetReadNow() {
		$redis = n_api('redis')->scrapy;
		$key = "readNow";
		$redis->del($key);
	}

	public function getAllArticleCount() {
		$articleCollection = $this->mongo->selectDB('weixin')->selectCollection('article');
		$stime = strtotime(date('Y-m-d', strtotime('-1 day')));
		$etime = $stime + 24 * 3600;
		$query = [
			'ptime' => ['$gte' => $stime, '$lt' => $etime],
		];
		$rs = $articleCollection->find($query)->count();
		echo $rs;
	}

	public function getWeixinUrlList()
	{
		$param = $this->param('clientIp:char:empty,skip:char:empty,limit:char:empty');
		$clientIp = $param['clientIp'];
        $skip = $param['skip'];
        $limit = $param['limit'];

		$articleCollection = $this->mongo->selectDB('weixin')->selectCollection('article');
		$readCollection = $this->mongo->selectDB('weixin')->selectCollection("read");

		$stime = strtotime(date('Y-m-d', strtotime('-1 day')));
		$etime = $stime + 24 * 3600;
		
		$query = [
			'ptime' => ['$gte' => $stime, '$lt' => $etime],
		];
		$cursor = $articleCollection->find($query, ['contentUrl'=>1,'wxid'=>1, 'isTop'=>1, 'ptime'=>1])->skip($skip)->limit($limit);
		$mongoidArr = [];
		foreach ($cursor as $value) {
			$mongoid = $value['_id']->{'$id'};
			$mongoidArr[] = $mongoid;
		}
		$rs = $readCollection->find(['articleId'=>['$in'=>$mongoidArr]], ['_id'=>0, 'articleId'=>1]);
		$idHave = [];
		foreach ($rs as $value) {
			$idHave[] = $value['articleId'];
		}
		$data = [];
		foreach ($cursor as $value) {
			$mongoid = $value['_id']->{'$id'};
			if (!in_array($mongoid, $idHave)) {
				$tmp['contentUrl'] = $value['contentUrl'];
				$tmp['mongoid'] = $mongoid;
				$tmp['wxid'] = $value['wxid'];
				$tmp['isTop'] = $value['isTop'];
				$tmp['ptime'] = $value['ptime'];
				$data[] = $tmp;
			}
		}
		echo json_encode($data);
	}

	public function getReadKey()
	{
		$redis = n_api('redis')->scrapy;
		$key = "readNow";
		if ($redis->exists($key))
			echo json_encode([]);
		else {
			$keyInfo = n_api('weixin')->getReadKeyInfo();
			echo json_encode($keyInfo);
		}
	}

	public function getReadKeyx()
	{
		$redis = n_api('redis')->scrapy;
		$key = "readNow";
		if ($redis->exists($key))
			echo json_encode([]);
		else {
			$keyInfo = n_api('weixin')->getReadKeyInfox();
			echo json_encode($keyInfo);
		}
	}

	public function getReadKeyRead() {
		$redis = n_api('redis')->scrapy;
		$key = "readNow";
		if ($redis->exists($key)) {
			$keyInfo = n_api('weixin')->getReadKeyInfo();
			echo json_encode($keyInfo);
		} else {
			echo json_encode([]);
		}
	}

	public function getReadKeyInside()
	{
		$keyInfo = n_api('weixin')->getReadKeyInfoInside();
		echo json_encode($keyInfo);
	}

	public function getReadKeyEvgo()
	{
		$keyInfo = n_api('weixin')->getReadKeyInfoEvgo();
		echo json_encode($keyInfo);
	}
}